package demo.practice.hash;

import java.util.*;

public class Ms1002 {

    public List<List<String>> groupAnagrams(String[] strs) {

        Map<String, List<String>> map = new HashMap<>();

        for (String s : strs) {
            char[] chars = s.toCharArray();
            Arrays.sort(chars);
            String key = new String(chars);
            if(map.get(key)!=null){
                map.get(key).add(s);
            }else{
                map.put(key,new ArrayList<>());
                map.get(key).add(s);
            }
        }

        Set<Map.Entry<String, List<String>>> entries = map.entrySet();
        List<List<String>> list= new ArrayList<>();
        for(Map.Entry<String, List<String>> en : entries){
            list.add(en.getValue());
        }
        return list;

    }
}
